package com.rear.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 时间转换工具类
 * @author wjz
 *
 */
public class DateUtil {
	public static int DATE_FORMAT = 2;
	public static Locale DATE_LOCALE = Locale.SIMPLIFIED_CHINESE;
	
	/**
	 * 测试方法
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(getCurrentWeekDay());
	}
	

	/**
	 * 获取中文时间戳 
	 * 2017年3月28日 星期二 上午11时49分47秒
	 * @return
	 */
	public static String getCurrentTimeStamp() {
		DateFormat df = DateFormat.getDateTimeInstance(0, 1, Locale.CHINA);
		return df.format(new java.util.Date(System.currentTimeMillis()));
	}

	/**
	 * 时间戳对比工具
	 * @param st1
	 * @param st2
	 * @return
	 */
	public static int compareTo(Timestamp st1, Timestamp st2) {
		long thisTime = (st1 != null) ? st1.getTime() : 0L;
		long anotherTime = (st2 != null) ? st2.getTime() : 0L;
		return ((thisTime == anotherTime) ? 0 : (thisTime < anotherTime) ? -1: 1);
	}

	/**
	 * 时间戳截取1 2017年3月28日 13:49:24 String
	 * @param ts
	 * @return
	 */
	public static String toDateTimeString(Timestamp ts) {
		if (ts == null) {
			return "";
		}
		return DateFormat.getDateTimeInstance(1, 2, Locale.CHINA).format(ts);
	}

	/**
	 * 时间戳截取 2017年3月28日 String
	 * @param ts
	 * @return
	 */
	public static String toDateString(Timestamp ts) {
		if (ts == null) {
			return "";
		}
		return DateFormat.getDateInstance(1, Locale.CHINA).format(ts);
	}

	/**
	 * 自定义 format时间 String
	 * @param ts
	 * @param format
	 * @return
	 */
	public static String toDateString(Timestamp ts, String format) {
		if (ts == null) {
			return "";
		}
		DateFormat df = new SimpleDateFormat(format);
		return df.format(ts);
	}

	/**
	 * 返回String型时间 String
	 * @param ts
	 * @return
	 */
	public static String toTimeString(Timestamp ts) {
		if (ts == null) {
			return "";
		}
		return DateFormat.getTimeInstance(1, Locale.CHINA).format(ts);
	}

	/**
	 * 返回long行时间 long
	 * @return
	 */
	public static long nowMillis() {
		return System.currentTimeMillis();
	}

	/**
	 * 原始时间戳 Timestamp
	 * @return
	 */
	public static Timestamp nowTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 获取时间 Date
	 * @return
	 */
	public static java.util.Date nowDate() {
		return new java.util.Date();
	}

	/**
	 * 获取当天0点 Timestamp
	 * @param stamp
	 * @return
	 */
	public static Timestamp getDayStart(Timestamp stamp) {
		return getDayStart(stamp, 0);
	}
	
	/**
	 * 获取N天后的0点 Timestamp
	 * @param stamp
	 * @return
	 */
	public static Timestamp getDayStart(Timestamp stamp, int daysLater) {
		Calendar tempCal = Calendar.getInstance();
		tempCal.setTime(new java.util.Date(stamp.getTime()));
		tempCal.set(tempCal.get(1), tempCal.get(2), tempCal.get(5), 0, 0, 0);

		tempCal.add(5, daysLater);
		return new Timestamp(tempCal.getTime().getTime());
	}
	/**
	 * 获取明天的0点 Timestamp
	 * @param stamp
	 * @return
	 */
	public static Timestamp getNextDayStart(Timestamp stamp) {
		return getDayStart(stamp, 1);
	}

	/**
	 * 获取当天的 23:59:59 Timestamp
	 * @param stamp
	 * @return
	 */
	public static Timestamp getDayEnd(Timestamp stamp) {
		return getDayEnd(stamp, 0);
	}

	/**
	 * 获取N天后的23:59:59 Timestamp
	 * @param stamp
	 * @return
	 */
	public static Timestamp getDayEnd(Timestamp stamp, int daysLater) {
		Calendar tempCal = Calendar.getInstance();
		tempCal.setTime(new java.util.Date(stamp.getTime()));
		tempCal.set(tempCal.get(1), tempCal.get(2), tempCal.get(5), 23, 59, 59);
		tempCal.add(5, daysLater);
		return new Timestamp(tempCal.getTime().getTime());
	}

	/**
	 * 时间转换  String To Timestamp
	 * @param dateString
	 * @return
	 */
	public static Timestamp toTimestamp(String dateString) {
		if ((dateString == null) || (dateString.trim().length() == 0)) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			java.util.Date date = format.parse(dateString.trim());
			return new Timestamp(date.getTime());
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 任意String时间 format 成Timestamp
	 * @param dateString
	 * @param timeFormat
	 * @return
	 */
	public static Timestamp parseTime(String dateString, String timeFormat) {
		if ((dateString == null) || (dateString.trim().length() == 0)) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(timeFormat);
		try {
			java.util.Date date = format.parse(dateString.trim());
			return new Timestamp(date.getTime());
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 任意String时间 format 成Date
	 * @param dateString
	 * @param timeFormat
	 * @return
	 */
	public static java.util.Date parseDate(String dateString, String timeFormat) {
		if ((dateString == null) || (dateString.trim().length() == 0)) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(timeFormat);
		try {
			java.util.Date date = format.parse(dateString.trim());
			return date;
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * String to Timestamp
	 * @param dateString
	 * @return
	 */
	public static Timestamp toTimestampAdjustTime(String dateString) {
		Timestamp st = toTimestamp(dateString);
		if (st != null) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(st);
			cal.set(10, 23);
			cal.set(12, 59);
			cal.set(13, 59);
			st.setTime(cal.getTime().getTime());

			return st;
		}
		return null;
	}

	/**
	 * 获取当月1日  Timestamp
	 * @return
	 */
	public static Timestamp monthBegin() {
		Calendar mth = Calendar.getInstance();
		mth.set(5, 1);
		mth.set(11, 0);
		mth.set(12, 0);
		mth.set(13, 0);
		mth.set(9, 0);
		return new Timestamp(mth.getTime().getTime());
	}

	/**
	 * 获取 时间 差 天  String
	 * @param st1
	 * @param st2
	 * @return
	 */
	public static String timestampDiff(Timestamp st1, Timestamp st2) {
		int count = timestampDiff(5, st1, st2);
		if (count < 0) {
			return null;
		}
		return count + " 天";
	}

	/**
	 * 时间戳的差异 int
	 * @param unit
	 * @param st1
	 * @param st2
	 * @return
	 */
	public static int timestampDiff(int unit, Timestamp st1, Timestamp st2) {
		if ((st1 == null) || (st2 == null)) {
			return -1;
		}

		Calendar g1 = Calendar.getInstance();
		Calendar g2 = Calendar.getInstance();
		g1.setTime(st1);
		g2.setTime(st2);
		Calendar gc2;
		Calendar gc1;
		if (g2.after(g1)) {
			gc2 = (Calendar) g2.clone();
			gc1 = (Calendar) g1.clone();
		} else {
			gc2 = (Calendar) g1.clone();
			gc1 = (Calendar) g2.clone();
		}

		if (5 == unit) {
			gc1.clear(14);
			gc1.clear(13);
			gc1.clear(12);
			gc1.clear(11);

			gc2.clear(14);
			gc2.clear(13);
			gc2.clear(12);
			gc2.clear(11);
		} else if (10 == unit) {
			gc1.clear(14);
			gc1.clear(13);
			gc1.clear(12);

			gc2.clear(14);
			gc2.clear(13);
			gc2.clear(12);
		} else if (12 == unit) {
			gc1.clear(14);
			gc1.clear(13);

			gc2.clear(14);
			gc2.clear(13);
		} else if (2 == unit) {
			gc1.clear(14);
			gc1.clear(13);
			gc1.clear(12);
			gc1.clear(11);
			gc1.clear(5);

			gc2.clear(14);
			gc2.clear(13);
			gc2.clear(12);
			gc2.clear(11);
			gc2.clear(5);
		} else if (1 == unit) {
			gc1.clear(14);
			gc1.clear(13);
			gc1.clear(12);
			gc1.clear(11);
			gc1.clear(5);
			gc1.clear(2);

			gc2.clear(14);
			gc2.clear(13);
			gc2.clear(12);
			gc2.clear(11);
			gc2.clear(5);
			gc2.clear(2);
		} else if (13 == unit) {
			gc1.clear(14);

			gc2.clear(14);
		} else {
			return 0;
		}

		int elapsed = 0;
		while (gc1.before(gc2)) {
			gc1.add(unit, 1);
			++elapsed;
		}
		return elapsed;
	}

	/**
	 * Date to yyyy-MM-dd HH:mm:ss String
	 * @param aDate
	 * @return
	 */
	public static String formatDatetime(java.util.Date aDate) {
		if (aDate == null)
			return "";
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(aDate);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = df.format(calendar.getTime());
		return s;
	}

	/**
	 * 得到日期间隔  天 小 时 分 秒  String
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 */
	public static String getDatesInterval(java.util.Date startDate,
			java.util.Date endDate) throws Exception {
		String start = formatDatetime(startDate);
		String end = formatDatetime(endDate);
		startDate = stringToDate(start);
		endDate = stringToDate(end);

		String interval = "";
		long miSecondInterval = endDate.getTime() - startDate.getTime();
		if (miSecondInterval < 0L)
			throw new Exception("开始时间不能在结束时间之后! endDate.getTime() = "
					+ endDate.getTime() + "; startDate.getTime() = "
					+ startDate.getTime() + "; miSecondInterval = "
					+ miSecondInterval);
		long ddInterval = miSecondInterval / 86400000L;
		long left = miSecondInterval % 86400000L;
		long hhInterval = left / 3600000L;
		left %= 3600000L;
		long mmInterval = left / 60000L;
		left %= 60000L;
		long ssInterval = left / 1000L;
		if (ddInterval != 0L)
			interval = interval + ddInterval + "天";
		if ((hhInterval != 0L) || (interval.length() != 0))
			interval = interval + hhInterval + "小时";
		if ((mmInterval != 0L) || (interval.length() != 0))
			interval = interval + mmInterval + "分";
		interval = interval + ssInterval + "秒";
		return interval;
	}

	/**
	 * 得到季度的最后日期 String
	 * @return
	 */
	public static String getLastDateOfQuarter() {
		String date = getCurrentYear();
		switch (Integer.parseInt(getCurrentQuarter())) {
		case 1:
			date = date + "-03-31";
			break;
		case 2:
			date = date + "-06-30";
			break;
		case 3:
			date = date + "-09-30";
			break;
		case 4:
			date = date + "-12-31";
		}

		return date;
	}

	/**
	 * 得到本月1日的日期 String
	 * @return
	 */
	public static String getFirstDateOfMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-");
		String s = df.format(nowDate());
		s = s + "01";
		return s;
	}
	/**
	 * 得到上个月的最后日期
	 * @return
	 */
	public static String getLastDateOfLastMonth() {
		DateFormat dateFormat = DateFormat.getDateInstance(DATE_FORMAT,
				DATE_LOCALE);
		java.util.Date uDate = null;
		try {
			uDate = dateFormat.parse(getFirstDateOfMonth());
		} catch (ParseException e) {
			System.err
					.println("Catch Exception in getLastDateOfLastMonth of DateUtil.");
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(uDate);
		calendar.add(5, -1);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String s = df.format(calendar.getTime());
		return s;
	}

	/**
	 * 获取 指定格式的日期 String
	 * @param type
	 * @return
	 */
	public static String getNextDate(int type) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(5, 1);
		SimpleDateFormat df ;
		switch (type) {
		case 0:
			df = new SimpleDateFormat("yyyy-MM-dd");
			break;
		case 1:
			df = new SimpleDateFormat("yyyy-MM-dd HH");
			break;
		case 2:
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			break;
		case 3:
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		case 4:
			df = new SimpleDateFormat("yyyyMMddHHmmss");
			break;
		case 5:
			df = new SimpleDateFormat("yyyyMMdd");
			break;
		case 6:
			df = new SimpleDateFormat("yyyyMM");
			break;
		default :
		df = new SimpleDateFormat("yyyy-MM-dd HH");
		
		}
		String s = df.format(calendar.getTime());
		return s;
	}

	/**
	 * 得到昨天的日期  yyyy-MM-dd String
	 * @return
	 */
	public static String getPreviousDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(5, -1);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String s = df.format(calendar.getTime());
		return s;
	}

	/**
	 * 获得月份 MM String
	 * @return
	 */
	public static String getCurrentMonth() {
		SimpleDateFormat df = new SimpleDateFormat("MM");
		String month = df.format(nowDate());
		return month;
	}

	/**
	 * 获得当前季度 String
	 * @return
	 */
	public static String getCurrentQuarter() {
		String quarter = null;
		Calendar mydate = Calendar.getInstance();
		Double dd = new Double(Math.floor(mydate.get(2) / 3));
		switch (dd.intValue()) {
		case 0:
			quarter = "1";
			break;
		case 1:
			quarter = "2";
			break;
		case 2:
			quarter = "3";
			break;
		case 3:
			quarter = "4";
		}

		return quarter;
	}

	/**
	 * 获取年份 String
	 * @return
	 */
	public static String getCurrentYear() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy");
		return df.format(nowDate());
	}

	
	/**
	 * 获取指定数据类型 的返回  sql.Date
	 * @param iType
	 * @return
	 */
	public static java.sql.Date getCurrentSqlDate(int iType) {
		String format = null;
		switch (iType) {
		case 0:
			format = "yyyy-MM-dd";
			break;
		case 1:
			format = "yyyy-MM-dd HH";
			break;
		case 2:
			format = "yyyy-MM-dd HH:mm";
			break;
		case 3:
		case 4:
		case 5:
			format = "yyyy-MM-dd HH:mm:ss";
		}

		return getSqlDate(getCurrentDate(iType), format);
	}

	/**
	 * 转换 指定类型 的返回  sql.Date
	 * @param iType
	 * @return
	 */
	public static java.sql.Date getSqlDate(String dateStr, String format) {
		DateFormat df = new SimpleDateFormat(format);
		java.sql.Date date = null;
		try {
			if ((dateStr != null) && (dateStr.trim().length() > 0))
				date = new java.sql.Date(df.parse(dateStr).getTime());
		} catch (ParseException e) {
			System.err.println("the given string cannot be parsed as a date : "
					+ dateStr + "|" + format);
		}
		return date;
	}

	/**
	 * 获取当前日期返回  String
	 * @return
	 */
	public static String getCurrentDate() {
		return getCurrentDate(0);
	}
	/**
	 * 获取当前日期指定格式 返回  String
	 * @return
	 */
	public static String getCurrentDate(int iType) {
		SimpleDateFormat df = null;

		switch (iType) {
		case 0:
			df = new SimpleDateFormat("yyyy-MM-dd");
			break;
		case 1:
			df = new SimpleDateFormat("yyyy-MM-dd HH");
			break;
		case 2:
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			break;
		case 3:
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		case 4:
			df = new SimpleDateFormat("yyyyMMddHHmmss");
			break;
		case 5:
			long currentTimeMillis = System.currentTimeMillis();
			Timestamp ts = new Timestamp(currentTimeMillis);
			return ts.toString();
		}
		return df.format(nowDate());
	}

	/**
	 * 获取当天日期
	 * @param timestamp
	 * @return
	 */
	public static String getDay(Timestamp timestamp) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(timestamp);
		return String.valueOf(calendar.get(5));
	}

	/**
	 * 获取当前  时间  月初  月中  月末
	 * @param timestamp
	 * @return
	 */
	public static String getPeriod(Timestamp timestamp) {
		int day = Integer.parseInt(getDay(timestamp));
		if ((day >= 1) && (day <= 10))
			return "1";
		if ((day >= 11) && (day <= 20)) {
			return "2";
		}
		return "3";
	}

	/**
	 * 输入String日期 获得季度 String
	 * @param sDate1
	 * @return
	 */
	public static String getQuarterByDay(String sDate1) {
		String quarter = "";
		DateFormat dateFormat = DateFormat.getDateInstance(DATE_FORMAT,
				DATE_LOCALE);
		java.util.Date date1 = null;
		try {
			date1 = dateFormat.parse(sDate1);
		} catch (ParseException e) {
			System.err
					.println("Catch Exception in getQuarterByDay(sDate1) sDate1:"
							+ sDate1);
			return "";
		}
		Calendar mydate = Calendar.getInstance();
		mydate.setTime(date1);
		Double dd = new Double(Math.floor(mydate.get(2) / 3));
		switch (dd.intValue()) {
		case 0:
			quarter = "1";
			break;
		case 1:
			quarter = "2";
			break;
		case 2:
			quarter = "3";
			break;
		case 3:
			quarter = "4";
		}

		return quarter;
	}
	/**
	 * 输入Timestamp日期 获得季度 String
	 * @param sDate1
	 * @return
	 */
	public static String getQuarter(Timestamp timestamp) {
		String quarter = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(timestamp);
		int month = calendar.get(2) + 1;
		if ((month == 1) || (month == 2) || (month == 3))
			quarter = "1";
		else if ((month == 4) || (month == 5) || (month == 6))
			quarter = "2";
		else if ((month == 7) || (month == 8) || (month == 9))
			quarter = "3";
		else if ((month == 10) || (month == 11) || (month == 12)) {
			quarter = "4";
		}
		return quarter;
	}

	/**
	 * 星期几 int
	 * @return
	 */
	public static int getCurrentWeekDay() {
		Calendar c = Calendar.getInstance();
		int num = c.get(7);
		int weekDayNum = 1;
		if (num == 1)
			weekDayNum = 7;
		else {
			weekDayNum = num - 1;
		}
		return weekDayNum;
	}

	/**
	 * 获得两个日期之前相差的天数 int
	 * 
	 * @param start
	 * @param e
	 *            DateUtil.getDaysInterval("2014-03-20 23:00:00",
	 *            "2014-03-26 21:00:00")
	 * @return
	 */
	public static int getDaysInterval(String startStr, String endStr) {
		Date startDate = stringToDate(startStr);
		Date endDate = stringToDate(endStr);
		if (startDate.after(endDate)) {
			Date t = startDate;
			startDate = endDate;
			endDate = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		int days = (int) ((endCalendar.getTimeInMillis() - startCalendar
				.getTimeInMillis()) / (3600L * 1000 * 24));
		return days;
	}

	/**
	 * 获得两个日期之前相差的月份 int
	 *  
	 * @param start
	 * @param e
	 * @return
	 */
	public static int getMonthsInterval(String startStr, String endStr) {
		Date startDate = stringToDate(startStr);
		Date endDate = stringToDate(endStr);
		if (startDate.after(endDate)) {
			Date t = startDate;
			startDate = endDate;
			endDate = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		Calendar temp = Calendar.getInstance();
		temp.setTime(endDate);
		temp.add(Calendar.DATE, 1);

		int year = endCalendar.get(Calendar.YEAR)
				- startCalendar.get(Calendar.YEAR);
		int month = endCalendar.get(Calendar.MONTH)
				- startCalendar.get(Calendar.MONTH);

		if ((startCalendar.get(Calendar.DATE) == 1)
				&& (temp.get(Calendar.DATE) == 1)) {
			return year * 12 + month + 1;
		} else if ((startCalendar.get(Calendar.DATE) != 1)
				&& (temp.get(Calendar.DATE) == 1)) {
			return year * 12 + month;
		} else if ((startCalendar.get(Calendar.DATE) == 1)
				&& (temp.get(Calendar.DATE) != 1)) {
			return year * 12 + month;
		} else {
			return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
		}
	}

	/**
	 * 获得两个日期之前相差的月份 int
	 * 
	 * @param start
	 * @param e
	 * @return
	 */
	public static int getYearsInterval(String startStr, String endStr) {
		Date startDate = stringToDate(startStr);
		Date endDate = stringToDate(endStr);
		if (startDate.after(endDate)) {
			Date t = startDate;
			startDate = endDate;
			endDate = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		Calendar temp = Calendar.getInstance();
		temp.setTime(endDate);
		temp.add(Calendar.DATE, 1);

		int year = endCalendar.get(Calendar.YEAR)
				- startCalendar.get(Calendar.YEAR);
		int month = endCalendar.get(Calendar.MONTH)
				- startCalendar.get(Calendar.MONTH);
		if (month > 0) {
			year = year + 1;
		}

		return year;
	}

	/**
	 * 计算开始日期days天之后的日期  String
	 * 
	 * @param startTime
	 *            开始日期
	 * @param days
	 *            天数
	 * @param format
	 *            格式
	 * @date 2012-04-24
	 * @return
	 */
	public static String getAddDates(Timestamp startTime, int days,
			String format) {
		if (startTime != null) {
			Timestamp endTime = DateUtil.getDayStart(startTime, days - 1);
			return DateUtil.toDateString(endTime, format);
		} else {
			return "";
		}
	}

	/**
	 * 计算开始日期days天之后的日期 String
	 * 
	 * @param startTime
	 *            开始日期
	 * @param days
	 *            天数
	 * @param format
	 *            格式
	 * @date 2012-04-24
	 * @return
	 */
	public static String getAddDates(String startDate, int days, String format) {
		if (startDate != null) {
			Timestamp startTime = DateUtil.toTimestamp(startDate);
			Timestamp endTime = DateUtil.getDayStart(startTime, days - 1);
			return DateUtil.toDateString(endTime, format);
		} else {
			return "";
		}
	}

	/**
	 * 计算开始日期days天之后的日期  String
	 * 
	 * @param startTime
	 *            开始日期 DateUtil.getAddDatesYes("2000-02-29 19:51:40", 1,
	 *            "yyyy-MM-dd HH:mm:ss")
	 * @param days
	 *            天数
	 * @param format
	 *            格式
	 * @date 2012-04-24
	 * @return
	 */
	public static String getAddDatesYes(String startDate, int days,
			String format) {
		if (startDate != null) {
			Timestamp startTime = DateUtil.toTimestamp(startDate);
			Timestamp endTime = DateUtil.getDayStart(startTime, days);
			return DateUtil.toDateString(endTime, format);
		} else {
			return "";
		}
	}

	/**
	 * 计算开始日期增加月数后的日期 String
	 * 
	 * @param startTime
	 *            DateUtil.getAddMonth("19990228 00:00:00", 1,
	 *            "yyyy-MM-dd HH:mm:ss") 开始日期
	 * @param month
	 *            月数
	 * @param format
	 *            返回格式
	 * @date 2012-04-24
	 * @return
	 */
	public static String getAddMonth(String startDate, int month, String format) {
		java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(format);
		if (startDate != null) {
			Calendar c = Calendar.getInstance();
			c.setTime(DateUtil.parseDate(startDate, "yyyyMMdd"));
			c.add(Calendar.MONTH, month);
			// c.add(Calendar.DAY_OF_YEAR, -1);
			Date endDate = c.getTime();
			return df.format(endDate);
		} else {
			return "";
		}
	}

	/**
	 * 修改指定时间的月份 String
	 * @param startDate
	 * @param month
	 * @param format
	 * @return
	 */
	public static String getAddMonth1(String startDate, int month, String format) {
		java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(format);
		if (startDate != null) {
			Calendar c = Calendar.getInstance();
			c.setTime(DateUtil.parseDate(startDate, "yyyy-MM-dd"));
			c.add(Calendar.MONTH, month);
			// c.add(Calendar.DAY_OF_YEAR, -1);
			Date endDate = c.getTime();
			return df.format(endDate);
		} else {
			return "";
		}
	}

	/**
	 * 计算开始日期增加年数后的日期  String
	 * 
	 * @param startTime
	 *            DateUtil.getAddYear("19990228 00:00:00", 1,
	 *            "yyyy-MM-dd HH:mm:ss") 开始日期
	 * @param month
	 *            月数
	 * @param format
	 *            返回格式
	 * @date 2012-04-24
	 * @return
	 */
	public static String getAddYear(String startDate, int year, String format) {
		java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(format);
		if (startDate != null) {
			Calendar c = Calendar.getInstance();
			c.setTime(DateUtil.parseDate(startDate, "yyyyMMdd"));
			c.add(Calendar.YEAR, year);
			Date endDate = c.getTime();
			return df.format(endDate);
		} else {
			return "";
		}
	}

	/**
	 * 根据传入的格式,将一个字符转换成个的Date对象,如果转换失败则返回默认的Date
	 * 
	 * @param v
	 *            要转换的字符串
	 * @param fm
	 *            格式
	 * @param def
	 *            默认的Date对象
	 * @return
	 */
	static public Date parse(String v, String fm, Date def) {
		if (v == null || v.length() == 0)
			return def;
		try {
			return new SimpleDateFormat(fm).parse(v);
		} catch (Exception e) {
			return def;
		}
	}

	/**
	 * 将日期制定格式化 String
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDate(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		return (new SimpleDateFormat(pattern)).format(date);
	}

	/**
	 * 把一个Date对象格式化成一个date_format日期字符串（不包括时间） String
	 * 
	 * @param tm
	 *            Date
	 * @param date_format
	 *            String 格式化样式
	 * @return String
	 */
	public static String formatDateToString(Date tm, String date_format) {
		if (tm == null) {
			return "";
		}
		return formatDate(tm, date_format);
	}

	/**
	 * 解析日期时间 Date
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static Date parseDateTime(String v, Date def) {
		return parse(v, "yyyy-MM-dd HH:mm:ss", def);
	}

	/**
	 * 获取服务器当前日期 Date
	 * 
	 * @return Date 服务器当前日期
	 */
	public static Date getNowDateByServer() {
		return new Date(System.currentTimeMillis());
	}

	/**
	 * 获取服务器当前SQL日期 sql.Date
	 * 
	 * @return Date 服务器当前日期
	 */
	public static java.sql.Date getNowSqlDateByServer() {
		return new java.sql.Date(System.currentTimeMillis());
	}

	/**
	 * 当前时间是否在设置时间内 （仅限时分秒) boolean
	 * 
	 * @param startTime
	 *            hh:mm:ss
	 * @param endTime
	 *            hh:mm:ss
	 * @return 在时间内返回true 否则返回false
	 */
	public static boolean chartTimeIn(String startTime, String endTime) {
		boolean isIn = false;
		Date start = DateUtil.parseDate(startTime, "hh:mm:ss");
		Date end = DateUtil.parseDate(endTime, "hh:mm:ss");
		Date now = DateUtil.parseDate(DateUtil.formatDatetime(new Date())
				.substring(11), "hh:mm:ss");
		if (now.getTime() >= start.getTime() && now.getTime() <= end.getTime()) {
			return true;
		}
		return isIn;
	}

	/**
	 * 校验两个时间的大小 boolean
	 * 
	 * @param startDateStr
	 * @param endDateStr
	 * @return
	 */
	public static boolean checkDateTime(String startDateStr, String endDateStr) {
		Date startDate = DateUtil
				.parseDate(startDateStr, "yyyy-MM-dd HH:mm:ss");
		Date endDate = DateUtil.parseDate(endDateStr, "yyyy-MM-dd HH:mm:ss");
		if (startDate.getTime() >= endDate.getTime()) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取当前时间 例13:50:20 String
	 * 
	 * @return
	 * @date 2015-04-23
	 */
	public static String getCurrentTime() {
		java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
				"HH:mm:ss");
		Date nowDate = getNowDateByServer();
		return df.format(nowDate);
	}


	/**
	 * 获取SimpleDateFormat  
	 * @param parttern 日期格式
	 * @return SimpleDateFormat对象
	 * @throws RuntimeException 异常：非法日期格式
	 */
	private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
		return new SimpleDateFormat(parttern);
	}

	/**
	 * 获取日期中的某数值。如获取月份 int
	 * @param date 日期
	 * @param dateType 日期格式
	 * @return 数值
	 */
	private static int getInteger(Date date, int dateType) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(dateType);
	}
	
	/**
	 * 增加日期中某类型的某数值。如增加日期 String
	 * @param date 日期字符串
	 * @param dateType 类型
	 * @param amount 数值
	 * @return 计算后日期字符串
	 */
	private static String addInteger(String date, int dateType, int amount) {
		String dateString = null;
		DateStyle dateStyle = getDateStyle(date);
		if (dateStyle != null) {
			Date myDate = stringToDate(date, dateStyle);
			myDate = addInteger(myDate, dateType, amount);
			dateString = dateToString(myDate, dateStyle);
		}
		return dateString;
	}
	
	/**
	 * 增加日期中某类型的某数值。如增加日期 Date
	 * @param date 日期
	 * @param dateType 类型
	 * @param amount 数值
	 * @return 计算后日期
	 */
	private static Date addInteger(Date date, int dateType, int amount) {
		Date myDate = null;
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(dateType, amount);
			myDate = calendar.getTime();
		}
		return myDate;
	}

	/**
	 * 获取精确的日期 Date
	 * @param timestamps 时间long集合
	 * @return 日期
	 */
	private static Date getAccurateDate(List<Long> timestamps) {
		Date date = null;
		long timestamp = 0;
		Map<Long, long[]> map = new HashMap<Long, long[]>();
		List<Long> absoluteValues = new ArrayList<Long>();

		if (timestamps != null && timestamps.size() > 0) {
			if (timestamps.size() > 1) {
				for (int i = 0; i < timestamps.size(); i++) {
					for (int j = i + 1; j < timestamps.size(); j++) {
						long absoluteValue = Math.abs(timestamps.get(i) - timestamps.get(j));
						absoluteValues.add(absoluteValue);
						long[] timestampTmp = { timestamps.get(i), timestamps.get(j) };
						map.put(absoluteValue, timestampTmp);
					}
				}

				// 有可能有相等的情况。如2012-11和2012-11-01。时间戳是相等的
				long minAbsoluteValue = -1;
				if (!absoluteValues.isEmpty()) {
					// 如果timestamps的size为2，这是差值只有一个，因此要给默认值
					minAbsoluteValue = absoluteValues.get(0);
				}
				for (int i = 0; i < absoluteValues.size(); i++) {
					for (int j = i + 1; j < absoluteValues.size(); j++) {
						if (absoluteValues.get(i) > absoluteValues.get(j)) {
							minAbsoluteValue = absoluteValues.get(j);
						} else {
							minAbsoluteValue = absoluteValues.get(i);
						}
					}
				}

				if (minAbsoluteValue != -1) {
					long[] timestampsLastTmp = map.get(minAbsoluteValue);
					if (absoluteValues.size() > 1) {
						timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
					} else if (absoluteValues.size() == 1) {
						// 当timestamps的size为2，需要与当前时间作为参照
						long dateOne = timestampsLastTmp[0];
						long dateTwo = timestampsLastTmp[1];
						if ((Math.abs(dateOne - dateTwo)) < 100000000000L) {
							timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
						} else {
							long now = new Date().getTime();
							if (Math.abs(dateOne - now) <= Math.abs(dateTwo - now)) {
								timestamp = dateOne;
							} else {
								timestamp = dateTwo;
							}
						}
					}
				}
			} else {
				timestamp = timestamps.get(0);
			}
		}

		if (timestamp != 0) {
			date = new Date(timestamp);
		}
		return date;
	}

	/**
	 * 判断字符串是否为日期字符串 boolean
	 * @param date 日期字符串
	 * @return true or false
	 */
	public static boolean isDate(String date) {
		boolean isDate = false;
		if (date != null) {
			if (stringToDate(date) != null) {
				isDate = true;
			}
		}
		return isDate;
	}

	/**
	 * 获取日期字符串的日期风格。失敗返回null。 DateStyle
	 * @param date 日期字符串
	 * @return 日期风格
	 */
	public static DateStyle getDateStyle(String date) {
		DateStyle dateStyle = null;
		Map<Long, DateStyle> map = new HashMap<Long, DateStyle>();
		List<Long> timestamps = new ArrayList<Long>();
		for (DateStyle style : DateStyle.values()) {
			Date dateTmp = stringToDate(date, style.getValue());
			if (dateTmp != null) {
				timestamps.add(dateTmp.getTime());
				map.put(dateTmp.getTime(), style);
			}
		}
		dateStyle = map.get(getAccurateDate(timestamps).getTime());
		return dateStyle;
	}

	/**
	 * 将日期字符串转化为日期。失败返回null。 Date
	 * @param date 日期字符串
	 * @return 日期
	 */
	public static Date stringToDate(String date) {
		DateStyle dateStyle = null;
		return stringToDate(date, dateStyle);
	}

	/**
	 * 将日期字符串转化为日期。失败返回null。 Date
	 * @param date 日期字符串
	 * @param parttern 日期格式
	 * @return 日期
	 */
	public static Date stringToDate(String date, String parttern) {
		Date myDate = null;
		if (date != null) {
			try {
				myDate = getDateFormat(parttern).parse(date);
			} catch (Exception e) {
			}
		}
		return myDate;
	}

	/**
	 * 将日期字符串转化为日期。失败返回null。 Date
	 * @param date 日期字符串
	 * @param dateStyle 日期风格
	 * @return 日期
	 */
	public static Date stringToDate(String date, DateStyle dateStyle) {
		Date myDate = null;
		if (dateStyle == null) {
			List<Long> timestamps = new ArrayList<Long>();
			for (DateStyle style : DateStyle.values()) {
				Date dateTmp = stringToDate(date, style.getValue());
				if (dateTmp != null) {
					timestamps.add(dateTmp.getTime());
				}
			}
			myDate = getAccurateDate(timestamps);
		} else {
			myDate = stringToDate(date, dateStyle.getValue());
		}
		return myDate;
	}

	/**
	 * 将日期转化为日期字符串。失败返回null。 String
	 * @param date 日期
	 * @param parttern 日期格式
	 * @return 日期字符串
	 */
	public static String dateToString(Date date, String parttern) {
		String dateString = null;
		if (date != null) {
			try {
				dateString = getDateFormat(parttern).format(date);
			} catch (Exception e) {
			}
		}
		return dateString;
	}

	/**
	 * 将日期转化为日期字符串。失败返回null。String
	 * @param date 日期
	 * @param dateStyle 日期风格
	 * @return 日期字符串
	 */
	public static String dateToString(Date date, DateStyle dateStyle) {
		String dateString = null;
		if (dateStyle != null) {
			dateString = dateToString(date, dateStyle.getValue());
		}
		return dateString;
	}

	/**
	 * 将日期字符串转化为另一日期字符串。失败返回null。 String
	 * @param date 旧日期字符串
	 * @param parttern 新日期格式
	 * @return 新日期字符串
	 */
	public static String stringToString(String date, String parttern) {
		return stringToString(date, null, parttern);
	}

	/**
	 * 将日期字符串转化为另一日期字符串。失败返回null。String
	 * @param date 旧日期字符串
	 * @param dateStyle 新日期风格
	 * @return 新日期字符串
	 */
	public static String stringToString(String date, DateStyle dateStyle) {
		return stringToString(date, null, dateStyle);
	}

	/**
	 * 将日期字符串转化为另一日期字符串。失败返回null。String
	 * @param date 旧日期字符串
	 * @param olddParttern 旧日期格式
	 * @param newParttern 新日期格式
	 * @return 新日期字符串
	 */
	public static String stringToString(String date, String olddParttern, String newParttern) {
		String dateString = null;
		if (olddParttern == null) {
			DateStyle style = getDateStyle(date);
			if (style != null) {
				Date myDate = stringToDate(date, style.getValue());
				dateString = dateToString(myDate, newParttern);
			}
		} else {
			Date myDate = stringToDate(date, olddParttern);
			dateString = dateToString(myDate, newParttern);
		}
		return dateString;
	}

	/**
	 * 将日期字符串转化为另一日期字符串。失败返回null。String
	 * @param date 旧日期字符串
	 * @param olddDteStyle 旧日期风格
	 * @param newDateStyle 新日期风格
	 * @return 新日期字符串
	 */
	public static String stringToString(String date, DateStyle olddDteStyle, DateStyle newDateStyle) {
		String dateString = null;
		if (olddDteStyle == null) {
			DateStyle style = getDateStyle(date);
			dateString = stringToString(date, style.getValue(), newDateStyle.getValue());
		} else {
			dateString = stringToString(date, olddDteStyle.getValue(), newDateStyle.getValue());
		}
		return dateString;
	}

	/**
	 * 增加日期的年份。失败返回null。String
	 * @param date 日期
	 * @param yearAmount 增加数量。可为负数
	 * @return 增加年份后的日期字符串
	 */
	public static String addYear(String date, int yearAmount) {
		return addInteger(date, Calendar.YEAR, yearAmount);
	}
	
	/**
	 * 增加日期的年份。失败返回null。 Date
	 * @param date 日期
	 * @param yearAmount 增加数量。可为负数
	 * @return 增加年份后的日期
	 */
	public static Date addYear(Date date, int yearAmount) {
		return addInteger(date, Calendar.YEAR, yearAmount);
	}
	
	/**
	 * 增加日期的月份。失败返回null。String
	 * @param date 日期
	 * @param yearAmount 增加数量。可为负数
	 * @return 增加月份后的日期字符串
	 */
	public static String addMonth(String date, int yearAmount) {
		return addInteger(date, Calendar.MONTH, yearAmount);
	}
	
	/**
	 * 增加日期的月份。失败返回null。Date
	 * @param date 日期
	 * @param yearAmount 增加数量。可为负数
	 * @return 增加月份后的日期
	 */
	public static Date addMonth(Date date, int yearAmount) {
		return addInteger(date, Calendar.MONTH, yearAmount);
	}
	
	/**
	 * 增加日期的天数。失败返回null。String
	 * @param date 日期字符串
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加天数后的日期字符串
	 */
	public static String addDay(String date, int dayAmount) {
		return addInteger(date, Calendar.DATE, dayAmount);
	}

	/**
	 * 增加日期的天数。失败返回null。Date
	 * @param date 日期
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加天数后的日期
	 */
	public static Date addDay(Date date, int dayAmount) {
		return addInteger(date, Calendar.DATE, dayAmount);
	}
	
	/**
	 * 增加日期的小时。失败返回null。String
	 * @param date 日期字符串
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加小时后的日期字符串
	 */
	public static String addHour(String date, int hourAmount) {
		return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
	}

	/**
	 * 增加日期的小时。失败返回null。Date
	 * @param date 日期
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加小时后的日期
	 */
	public static Date addHour(Date date, int hourAmount) {
		return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
	}
	
	/**
	 * 增加日期的分钟。失败返回null。String
	 * @param date 日期字符串
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加分钟后的日期字符串
	 */
	public static String addMinute(String date, int hourAmount) {
		return addInteger(date, Calendar.MINUTE, hourAmount);
	}

	/**
	 * 增加日期的分钟。失败返回null。Date
	 * @param date 日期
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加分钟后的日期
	 */
	public static Date addMinute(Date date, int hourAmount) {
		return addInteger(date, Calendar.MINUTE, hourAmount);
	}
	
	/**
	 * 增加日期的秒钟。失败返回null。String
	 * @param date 日期字符串
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加秒钟后的日期字符串
	 */
	public static String addSecond(String date, int hourAmount) {
		return addInteger(date, Calendar.SECOND, hourAmount);
	}

	/**
	 * 增加日期的秒钟。失败返回null。Date
	 * @param date 日期
	 * @param dayAmount 增加数量。可为负数
	 * @return 增加秒钟后的日期
	 */
	public static Date addSecond(Date date, int hourAmount) {
		return addInteger(date, Calendar.SECOND, hourAmount);
	}

	/**
	 * 获取日期的年份。失败返回0。 int
	 * @param date 日期字符串
	 * @return 年份
	 */
	public static int getYear(String date) {
		return getYear(stringToDate(date));
	}

	/**
	 * 获取日期的年份。失败返回0。int
	 * @param date 日期
	 * @return 年份
	 */
	public static int getYear(Date date) {
		return getInteger(date, Calendar.YEAR);
	}

	/**
	 * 获取日期的月份。失败返回0。int
	 * @param date 日期字符串
	 * @return 月份
	 */
	public static int getMonth(String date) {
		return getMonth(stringToDate(date));
	}

	/**
	 * 获取日期的月份。失败返回0。 int
	 * @param date 日期
	 * @return 月份
	 */
	public static int getMonth(Date date) {
		return getInteger(date, Calendar.MONTH);
	}

	/**
	 * 获取日期的天数。失败返回0。 int
	 * @param date 日期字符串
	 * @return 天
	 */
	public static int getDay(String date) {
		return getDay(stringToDate(date));
	}

	/**
	 * 获取日期的天数。失败返回0。int
	 * @param date 日期
	 * @return 天
	 */
	public static int getDay(Date date) {
		return getInteger(date, Calendar.DATE);
	}
	
	/**
	 * 获取日期的小时。失败返回0。int
	 * @param date 日期字符串
	 * @return 小时
	 */
	public static int getHour(String date) {
		return getHour(stringToDate(date));
	}

	/**
	 * 获取日期的小时。失败返回0。int
	 * @param date 日期
	 * @return 小时
	 */
	public static int getHour(Date date) {
		return getInteger(date, Calendar.HOUR_OF_DAY);
	}
	
	/**
	 * 获取日期的分钟。失败返回0。int
	 * @param date 日期字符串
	 * @return 分钟
	 */
	public static int getMinute(String date) {
		return getMinute(stringToDate(date));
	}

	/**
	 * 获取日期的分钟。失败返回0。int
	 * @param date 日期
	 * @return 分钟
	 */
	public static int getMinute(Date date) {
		return getInteger(date, Calendar.MINUTE);
	}
	
	/**
	 * 获取日期的秒钟。失败返回0。int
	 * @param date 日期字符串
	 * @return 秒钟
	 */
	public static int getSecond(String date) {
		return getSecond(stringToDate(date));
	}

	/**
	 * 获取日期的秒钟。失败返回0。int
	 * @param date 日期
	 * @return 秒钟
	 */
	public static int getSecond(Date date) {
		return getInteger(date, Calendar.SECOND);
	}

	/**
	 * 获取日期 。默认yyyy-MM-dd格式。失败返回null。String
	 * @param date 日期字符串
	 * @return 日期
	 */
	public static String getDate(String date) {
		return stringToString(date, DateStyle.YYYY_MM_DD);
	}

	/**
	 * 获取日期。默认yyyy-MM-dd格式。失败返回null。String
	 * @param date 日期
	 * @return 日期
	 */
	public static String getDate(Date date) {
		return dateToString(date, DateStyle.YYYY_MM_DD);
	}

	/**
	 * 获取日期的时间。默认HH:mm:ss格式。失败返回null。String
	 * @param date 日期字符串
	 * @return 时间
	 */
	public static String getTime(String date) {
		return stringToString(date, DateStyle.HH_MM_SS);
	}

	/**
	 * 获取日期的时间。默认HH:mm:ss格式。失败返回null。String
	 * @param date 日期
	 * @return 时间
	 */
	public static String getTime(Date date) {
		return dateToString(date, DateStyle.HH_MM_SS);
	}

	/**
	 * 获取日期的星期。失败返回null。Week
	 * @param date 日期字符串
	 * @return 星期
	 */
	public static Week getWeek(String date) {
		Week week = null;
		DateStyle dateStyle = getDateStyle(date);
		if (dateStyle != null) {
			Date myDate = stringToDate(date, dateStyle);
			week = getWeek(myDate);
		}
		return week;
	}
	
	/**
	 *  将字符串转换成日期Time Long值 
	 * @param date
	 * @param dateStyle
	 * @return
	 */
	public static long stringToDataLong(String date, DateStyle dateStyle){
		Date dateTmp = stringToDate(date,dateStyle);
		return dateTmp.getTime();
	}

	/**
	 * 将YYYYMMDD格式字符串转成Time Long值 
	 * @param date
	 * @return
	 */
	public static long yyyyMMddToDataLong(String date){
		Date dateTmp = stringToDate(date, DateStyle.YYYY_MM_DD);
		if(null == dateTmp){
			return 0;
		}
		return dateTmp.getTime();
	}

	/**
	 * 将YYYYMMDD格式字符串转成开始时间的Time Date值，默认为当天的0点0时0分 
	 * @param date
	 * @return
	 */
	public static Date yyyyMMddToStartDataLong(String date){
		Date dateTmp = stringToDate(date+" 00:00:00", DateStyle.YYYY_MM_DD_HH_MM_SS);
		if(null == dateTmp){
			return dateTmp ;
		}
		return dateTmp;
	}
	
	/**
	 * 将YYYYMMDD格式字符串转成结束时间的Time Date值，默认为当天下一天的0点0时0秒
	 * @param date
	 * @return
	 */
	public static Date yyyyMMddToEndDataLong(String date){
		Date dateTmp = stringToDate(date+" 00:00:00", DateStyle.YYYY_MM_DD_HH_MM_SS);
		if(null == dateTmp){
			return dateTmp;
		}
		return addDay(dateTmp, 1);
	}
	
	/**
	 * 将日期long值转换成指定的日期格式字符串 String
	 * @param dateLong
	 * @param parttern
	 * @return
	 */
	public static String dateLongToString(long dateLong, String parttern){
		Date dateTmp = new Date(dateLong);
		return dateToString(dateTmp, parttern);
	}
	/**
	 * 获取日期的星期。失败返回null。Week
	 * @param date 日期
	 * @return 星期
	 */
	public static Week getWeek(Date date) {
		Week week = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int weekNumber = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		switch (weekNumber) {
		case 0:
			week = Week.SUNDAY;
			break;
		case 1:
			week = Week.MONDAY;
			break;
		case 2:
			week = Week.TUESDAY;
			break;
		case 3:
			week = Week.WEDNESDAY;
			break;
		case 4:
			week = Week.THURSDAY;
			break;
		case 5:
			week = Week.FRIDAY;
			break;
		case 6:
			week = Week.SATURDAY;
			break;
		default:;
		}
		return week;
	}
	
	/**
	 * 获取两个日期相差的天数 int
	 * @param date 日期字符串
	 * @param otherDate 另一个日期字符串
	 * @return 相差天数
	 */
	public static int getIntervalDays(String date, String otherDate) {
		return getIntervalDays(stringToDate(date), stringToDate(otherDate));
	}
	
	/**
	 * @param date 日期int
	 * @param otherDate 另一个日期
	 * @return 相差天数
	 */
	public static int getIntervalDays(Date date, Date otherDate) {
		date = stringToDate(DateUtil.getDate(date));
		long time = Math.abs(date.getTime() - otherDate.getTime());
		return (int)time/(24 * 60 * 60 * 1000);
	}
	

	/**
	 * 内部日期格式枚举类，主要校验输入的日期格式是否正确
	 * @author wjz
	 *
	 */
	public enum DateStyle {
		
		MM_DD("MM-dd"),
		YYYY_MM("yyyy-MM"),
		YYYY_MM_DD("yyyy-MM-dd"),
		MM_DD_HH_MM("MM-dd HH:mm"),
		MM_DD_HH_MM_SS("MM-dd HH:mm:ss"),
		YYYY_MM_DD_HH_MM("yyyy-MM-dd HH:mm"),
		YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss"),
		
		MM_DD_EN("MM/dd"),
		YYYY_MM_EN("yyyy/MM"),
		YYYY_MM_DD_EN("yyyy/MM/dd"),
		MM_DD_HH_MM_EN("MM/dd HH:mm"),
		MM_DD_HH_MM_SS_EN("MM/dd HH:mm:ss"),
		YYYY_MM_DD_HH_MM_EN("yyyy/MM/dd HH:mm"),
		YYYY_MM_DD_HH_MM_SS_EN("yyyy/MM/dd HH:mm:ss"),
		
		MM_DD_CN("MM月dd日"),
		YYYY_MM_CN("yyyy年MM月"),
		YYYY_MM_DD_CN("yyyy年MM月dd日"),
		MM_DD_HH_MM_CN("MM月dd日 HH:mm"),
		MM_DD_HH_MM_SS_CN("MM月dd日 HH:mm:ss"),
		YYYY_MM_DD_HH_MM_CN("yyyy年MM月dd日 HH:mm"),
		YYYY_MM_DD_HH_MM_SS_CN("yyyy年MM月dd日 HH:mm:ss"),
		
		HH_MM("HH:mm"),
		HH_MM_SS("HH:mm:ss");
		
		
		private String value;
		
		DateStyle(String value) {
			this.value = value;
		}
		
		public String getValue() {
			return value;
		}
	}
	
	/**
	 * 内部星期枚举类，主要用于转换日期格式
	 * @author wjz
	 *
	 */
	private enum Week {

		MONDAY("星期一", "Monday", "Mon.", 1),
		TUESDAY("星期二", "Tuesday", "Tues.", 2),
		WEDNESDAY("星期三", "Wednesday", "Wed.", 3),
		THURSDAY("星期四", "Thursday", "Thur.", 4),
		FRIDAY("星期五", "Friday", "Fri.", 5),
		SATURDAY("星期六", "Saturday", "Sat.", 6),
		SUNDAY("星期日", "Sunday", "Sun.", 7);
		
		String name_cn;
		String name_en;
		String name_enShort;
		int number;
		
		Week(String name_cn, String name_en, String name_enShort, int number) {
			this.name_cn = name_cn;
			this.name_en = name_en;
			this.name_enShort = name_enShort;
			this.number = number;
		}
		
		private String getChineseName() {
			return name_cn;
		}

		public String getName() {
			return name_en;
		}

		public String getShortName() {
			return name_enShort;
		}

		public int getNumber() {
			return number;
		}
	}
	/**
	 * 根据季度转换每个季度的开始时间 String
	 * @param quarter
	 * @param year
	 * @return
	 */
	public static String quarterToStartTime(String quarter,String year){
		String startTime = "";
		switch (quarter) {
		case "1":
			startTime = year+"-01-01";
			break;
		case "2":
			startTime = year+"-04-01";
			break;
		case "3":
			startTime = year+"-07-01";
			break;
		case "4":
			startTime = year+"-10-01";
			break;
		case "5":
			startTime = year+"-01-01";
			break;
		case "6":
			startTime = year+"-07-01";
			break;

		default:
			startTime = year+"-01-01";
			break;
		}
		return startTime;
	}
	/**
	 * 根据季度转换每个季度的结束时间 String
	 * @param quarter
	 * @param year
	 * @return
	 */
	public static String quarterToEndTime(String quarter,String year){
		String  endTime = "";
		switch (quarter) {
		case "1":
			endTime = year+"-03-30";
			break;
		case "2":
			endTime = year+"-06-30";
			break;
		case "3":
			endTime = year+"-09-30";
			break;
		case "4":
			endTime = year+"-12-31";
			break;
		case "5":
			endTime = year+"-06-30";
			break;
		case "6":
			endTime = year+"-12-31";
			break;
		default:
			endTime = year+"-12-31";
			break;
		}
		return endTime;
	}
}

